package nl.rainbowsheep.reisorg.rest.service.service;

import java.util.ArrayList;
import java.util.List;
import nl.rainbowsheep.reisorg.rest.service.model.BurritoUser;
import nl.rainbowsheep.reisorg.rest.service.model.Customer;
import nl.rainbowsheep.reisorg.rest.service.repository.CustomerRepository;
import nl.rainbowsheep.reisorg.rest.service.viewmodel.CustomerViewmodel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service class for customers
 */
@Service
public class CustomerService {

    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private UserService userService;

    /**
     * use slf4j logger during debugging
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerService.class);

    public Page<Customer> findAll(Pageable pageable) {
        return customerRepository.findAll(pageable);
    }

    @Transactional
    public CustomerViewmodel save(CustomerViewmodel customerviewmodel) {
        Customer customer = customerviewmodel.getCustomer();
        Customer newCustomer = customerRepository.save(customer);
        CustomerViewmodel newCustomerViewmodel = new CustomerViewmodel();
        BurritoUser bu = userService.findByBurritoUserId(newCustomer.getBurritoUserId());
        newCustomerViewmodel.setCustomer(newCustomer);
        newCustomerViewmodel.setBurritoUser(bu);
        newCustomerViewmodel.setTravelGroups(customerviewmodel.getTravelGroups());
        return newCustomerViewmodel;
    }
//
//    @Transactional
//    public Customer saveCustomerViewmodel(CustomerViewmodel customerViewmodel) {
//        Customer customer = new Customer();
//        BurritoUser burritoUser = userService.findByUsername(customerViewmodel.getBurritoUser().getUsername());
////        burritoUser.setPassword(customerViewmodel.getBurritoUser().getPassword());
////        burritoUser.setUsername(customerViewmodel.getBurritoUser().getUsername());
////        burritoUser.setBurritoUserRoles(customerViewmodel.getBurritoUser().getBurritoUserRoles());
////        burritoUser.setEnabled(true);
////        BurritoUser burritoUserSaved = userService.save(burritoUser);
//        long burritoUserId = burritoUser.getId();
//        customer.setBurritoUserId(burritoUserId);
////        customer.setBirthdate(customerViewmodel.getBirthdate());
////        customer.setCity(customerViewmodel.getCity());
////        customer.setEmail(customerViewmodel.getEmail());
////        customer.setSurname(customerViewmodel.getSurname());
////        customer.setLastname(customerViewmodel.getLastname());
////        customer.setExtraAddress(customerViewmodel.getExtraAddress());
////        customer.setGender(customerViewmodel.getGender());
////        customer.setHouseNumber(customerViewmodel.getHouseNumber());
////        customer.setIsActive(true);
////        customer.setPostalCode(customerViewmodel.getPostalCode());
////        customer.setRegistrationDate(customerViewmodel.getRegistrationDate());
////        customer.setStreetAddress(customerViewmodel.getStreetAddress());
////        customer.setTelephone1(customerViewmodel.getTelephone1());
////        customer.setTelephone2(customerViewmodel.getTelephone2());
//        
//        return customerRepository.save(customer);
//    }

    public Customer getCustomerById(Long id) throws Exception {
        if (customerRepository.exists(id)) {
            return customerRepository.findOne(id);
        } else {
            throw new Exception();
        }
    }

    public CustomerViewmodel getCustomerViewmodelByUsername(String username) {
        CustomerViewmodel customerViewmodel = null;
        BurritoUser burritoUser = userService.findByUsername(username);
        if (burritoUser != null) {
            customerViewmodel = new CustomerViewmodel();
            Customer customer = customerRepository.findByBurritoUserId(burritoUser.getId());
            customerViewmodel.setCustomer(customer);
            customerViewmodel.setBurritoUser(burritoUser);
            customerViewmodel.setTravelGroups(customer.getTravelgroups());
        }
        return customerViewmodel;
    }

    public Customer getCustomerByUsername(String username) {
        Customer customer = null;
        BurritoUser burritoUser = userService.findByUsername(username);
        if (burritoUser != null) {
            customer = customerRepository.findByBurritoUserId(burritoUser.getId());
        }
        return customer;
    }

    public List<Customer> findCustomers(Customer customer) {
        return customerRepository.findByLastnameLike(customer.getLastname());
    }
}
